<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="标题"></el-input>
    </el-form-item>
    <el-form-item label="朋友圈内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="朋友圈内容"></el-input>
    </el-form-item>
    <el-form-item label="朋友圈的观看数量" prop="view">
      <el-input v-model="dataForm.view" placeholder="朋友圈的观看数量"></el-input>
    </el-form-item>
    <el-form-item label="盆友圈的评论数量" prop="commentNum">
      <el-input v-model="dataForm.commentNum" placeholder="盆友圈的评论数量"></el-input>
    </el-form-item>
    <el-form-item label="发布者" prop="publisher">
      <el-input v-model="dataForm.publisher" placeholder="发布者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          content: '',
          view: '',
          commentNum: '',
          publisher: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '朋友圈内容不能为空', trigger: 'blur' }
          ],
          view: [
            { required: true, message: '朋友圈的观看数量不能为空', trigger: 'blur' }
          ],
          commentNum: [
            { required: true, message: '盆友圈的评论数量不能为空', trigger: 'blur' }
          ],
          publisher: [
            { required: true, message: '发布者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/lovegoing_admin/state/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.state.title
                this.dataForm.content = data.state.content
                this.dataForm.view = data.state.view
                this.dataForm.commentNum = data.state.commentNum
                this.dataForm.publisher = data.state.publisher
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/lovegoing_admin/state/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'content': this.dataForm.content,
                'view': this.dataForm.view,
                'commentNum': this.dataForm.commentNum,
                'publisher': this.dataForm.publisher
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
